<html>

{%- macro code(text) -%}
<span class="code">{{ text }}</span>
{%- endmacro -%}

{% macro avro_field(schema, display_record_header=True) %}
  {# PRIMITIVE TYPES #}
  {% if schema in ('null', 'boolean', 'int', 'long', 'float', 'double', 'bytes', 'string', 'urn', 'uri') %}
    {{ code(schema) }}
  {% elif schema is instanceof('') %}
    {{ avro_link(schema) }}
  {% elif schema is instanceof([]) %}
    Union:<br>{% for s in schema %}{{ avro_field(s) }}{% if not loop.last %},<br>{% endif %}{% endfor %}
  {% elif schema.type == 'enum' %}
    Enum {{ code(schema.name) }}:<br>{% for sym in schema.symbols %}{{ code(sym) }}{% if not loop.last %},<br>{% endif %}{% endfor %}
    {% if schema.doc %}{{ schema.doc }}<br><br>{% endif %}
  {% elif schema.type == 'array' %}
    Array: {{ avro_field(schema['items']) }}
    {% if schema.doc %}{{ schema.doc }}<br><br>{% endif %}
  {% elif schema.type == 'map' %}
    Map: {{ code('string') }} to {{ avro_field(schema.values) }}
    {% if schema.doc %}{{ schema.doc }}<br><br>{% endif %}
  {% elif schema.type == 'record' %}
    {% if display_record_header %}
    Record: {{ code(schema.name) }}<br><br>
    {% if schema.doc %}{{ schema.doc|join|safe }}<br><br>{% endif %}
    {% endif %}
    <table>
      <thead>
      <tr>
        <th>Name</th>
        <th>Type</th>
        <th>Default</th>
        <th>Notes</th>
      </tr>
      </thead>
      <tbody>
      {% for field in schema.fields %}
      <tr>
        <td>{{ field.name }}</td>
        <td>{{ avro_field(field if field.type in ('enum', 'array', 'map', 'record') else field.type) }}</td>
        <td>{{ code(field.default) }}</td>
        <td>{% if not field.has_key('default') %}<b>REQUIRED</b> {% endif %}{{ field.doc|join|safe }}</td>
      </tr>
      {% endfor %}
      </tbody>
    </table>
  {% endif %}
{% endmacro %}

{% macro avro_link(entity_name) %}
  <a href="{{ url_for('read_model', model_id=entity_name) }}">{{ code(entity_name) }}</a>
{% endmacro %}

{% macro display_links(links) %}
<table>
  <thead>
  <tr>
    <th>Rel</th>
    <th>Notes</th>
  </tr>
  </thead>
  <tbody>
  {% for link in links %}
  <tr>
    <td>{{ link.rel }}</td>
    <td>{{ link.doc|join|safe }}</td>
  {% endfor %}
  </tr>
  </tbody>
</table>
{% endmacro %}

</html>